package com.imodia.checkoo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsImageSourceBean;
import com.imodia.checkoo.bean.CmsManagerMenuBean;
import com.imodia.checkoo.service.impl.CmsManagerMenuServiceImpl;

@Controller
@RequestMapping("/hfmanager/manager_menu/list.do")
public class CmsManagerMenuController {
@SuppressWarnings("unused")
private final Log log = LogFactory.getLog(CmsManagerMenuController.class);
	
	private final String baseUrl = "manager_menu";
	
	private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
	private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
	private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";
	private final String url_list4acticle = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list4acticle";
	private final String url_menu_set = "/hfmanager/" + baseUrl + "/menu_set";

	@Autowired
	private CmsManagerMenuServiceImpl cmsManagerMenuService;
	
	@RequestMapping(params = "action=delete")
	public ModelAndView delete(HttpServletRequest request) {
		int id = StringUtil.requestInt(request,"id");
		cmsManagerMenuService.delete(id);
		return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
	}

	@RequestMapping(params = "action=save")
	public ModelAndView edit(@ModelAttribute("userMessage")CmsManagerMenuBean obj, HttpServletRequest request) {
		int x = 0;
		if(obj.getId()!=null && obj.getId()>0){
			x = cmsManagerMenuService.update(obj);
		}else{
			x = cmsManagerMenuService.insert(obj);
			obj.setId(x);
		}
		if(x>0){
			x = 1;
		}
		return new ModelAndView(new RedirectView(request.getContextPath()+"/hfmanager/" +baseUrl + "/list.do?action=edit&x="+x+"&id="+obj.getId()), null);
	}
	
	@RequestMapping(params = "action=edit")
	public ModelAndView edit_in(HttpServletRequest request,CmsManagerMenuBean obj) {
		int id = StringUtil.requestInt(request,"id");
		Map<String,Object> m = new HashMap<String,Object>();
		CmsManagerMenuBean info = cmsManagerMenuService.get(id);
		if(info==null)info=obj;
		m.put("info", info);
		m.put("parent", cmsManagerMenuService.getManageMenu(8));
		
	    return new ModelAndView(url_edit, m);
	}
	
	@RequestMapping(params = "action=list")
	public ModelAndView list(HttpServletRequest request, CmsManagerMenuBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("info", cmsManagerMenuService.getManageMenu(0));
		return new ModelAndView(url_list, model);
	}
	
	@RequestMapping(params = "action=list4acticle")
	public ModelAndView list4acticle(HttpServletRequest request, CmsManagerMenuBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();

		PageResult<CmsManagerMenuBean> pageResult =cmsManagerMenuService.page(new SqlParam(),page);
		model.put("page", pageResult);
		
		return new ModelAndView(url_list4acticle, model);
	}
	
	@RequestMapping(params = "action=departmentMenuList")
	public ModelAndView departmentMenuList(HttpServletRequest request, CmsManagerMenuBean obj, PageBean page) {
		HashMap<String, Object> model = new HashMap<String, Object>();
		model.put("info", cmsManagerMenuService.getManageMenu(0));
		return new ModelAndView(url_menu_set, model);
	}
	
	
	

}
